package com.liang.config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

//    授权
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
//                请求授权的规则
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

//      没有权限默认到login页面
        http.formLogin().loginPage("/toLogin").usernameParameter("username").passwordParameter("password");
//
//防止网站工具： post get
        http.csrf().disable();//关闭csrf

//        注销
        http.logout().logoutSuccessUrl("/");

        //开启记住我功能,默认保存两周
        http.rememberMe().rememberMeParameter("remember");

    }



//    认证
   protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("liangcheng").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2")
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("root")).roles("vip1","vip3");
    }


}
